package de.hwrberlin.remo.diagram.custom.decorators;

import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.common.core.service.AbstractProvider;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.CreateDecoratorsOperation;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorProvider;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;

import de.hwrberlin.remo.model.Fact;
import de.hwrberlin.remo.model.Rule;


public class ElementStateDecoratorProvider extends AbstractProvider implements
		IDecoratorProvider {

	@Override
	public void createDecorators(IDecoratorTarget decoratorTarget) {
		
		EditPart ep = (EditPart) decoratorTarget.getAdapter(EditPart.class);
		if (ep != null && ep instanceof IPrimaryEditPart) {
			GraphicalEditPart gEP = (GraphicalEditPart) ep;
			if (gEP.hasNotationView()) {
				decoratorTarget.installDecorator("FACT_STATE_DECORATOR",
						new ElementStateDecorator(decoratorTarget));
			}
		}
	}

	@Override
	public boolean provides(IOperation operation) {
		if (!(operation instanceof CreateDecoratorsOperation)) {
			return false;
		}

		IDecoratorTarget decoratorTarget = ((CreateDecoratorsOperation) operation)
				.getDecoratorTarget();
		EditPart ep = (EditPart) decoratorTarget.getAdapter(EditPart.class);
		
		if (ep instanceof GraphicalEditPart && ep instanceof IPrimaryEditPart) {
			GraphicalEditPart gEP = (GraphicalEditPart) ep;
			if (gEP.hasNotationView()) {
				return (gEP.resolveSemanticElement() instanceof Fact || 
						gEP.resolveSemanticElement() instanceof Rule);
			}
		}
		return false;
	}
}
